package com.example.lottery.service.activitystatus.operater;

import com.example.lottery.dao.datobject.ActivityDO;
import com.example.lottery.dao.mapper.ActivityMapper;
import com.example.lottery.dao.mapper.ActivityPrizeMapper;
import com.example.lottery.service.dto.ConvertActivityStatusDTO;
import com.example.lottery.service.enums.ActivityPrizeStatusEnum;
import com.example.lottery.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetStatus=convertActivityStatusDTO.getTargetActivityStatus();
        if(activityId==null||targetStatus==null){
            return false;
        }
        ActivityDO activityDO=activityMapper.selectById(activityId);
        if(activityDO==null){
            return false;
        }
        //如果当前的活动状态与传入的活动状态一致 也不进行处理
        if(targetStatus.name().equalsIgnoreCase(activityDO.getStatus())){
            return false;
        }
        //需要判断奖品是否全部抽完
        //查询INIT状态的奖品个数
        int count=activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if(count>0){
            return false;
        }
        return true;

    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
       try{
           activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                   convertActivityStatusDTO.getTargetActivityStatus().name());
           return true;

       }catch (Exception e){
           return false;
       }
    }
}
